<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeNo">
              <a-input v-model="model.typeNo" placeholder="请输入编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeName">
              <a-input v-model="model.typeName" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="出入标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ioFlag">
              <j-dict-select-tag type="list" v-model="model.ioFlag" dictCode="mm_io_flag" placeholder="请选择出入标志" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="反向移动类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reverseId">
              <j-dict-select-tag type="list" v-model="model.reverseId" dictCode="mdm_movement_type,type_name,id" placeholder="请选择反向移动类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="冲销标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cancelFlag">
              <j-switch v-model="model.cancelFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其它出入库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherFlag">
              <j-switch v-model="model.otherFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="借贷" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dcFlag">-->
<!--              <j-dict-select-tag type="list" v-model="model.dcFlag" dictCode="mdm_dc_flag" placeholder="请选择借贷标识" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="凭证类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="voucherType">-->
<!--              <j-dict-select-tag type="list" v-model="model.voucherType" dictCode="mdm_voucher_type" placeholder="请选择凭证类型" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="凭证更新" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="voucherFlag">-->
<!--              <j-dict-select-tag type="radio" v-model="model.voucherFlag" dictCode="yn" placeholder="请选择凭证更新" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="事务关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessKey">-->
<!--              <a-input v-model="model.businessKey" placeholder="请输入事务关键字"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="科目修改" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accountKey">-->
<!--              <a-input v-model="model.accountKey" placeholder="请输入科目修改"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MdmMovementTypeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            enableFlag:"1",
            cancelFlag:"0",
            otherFlag:"0",
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           typeNo: [
             { required: true, message: '请输入编码!'},
             { validator: (rule, value, callback) => validateDuplicateValue('mdm_movement_type', 'type_no', value, this.model.id, callback)},
           ],
           typeName: [
              { required: true, message: '请输入名称!'},
           ],
          ioFlag: [
            { required: true, message: '请选择出入标志!'},
          ],
        },
        url: {
          add: "/mdm/movementType/add",
          edit: "/mdm/movementType/edit",
          queryById: "/mdm/movementType/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>